// pages/notice/notice.ts
//import { areaList } from '@vant/area-data';
import { getChinaProvince, getCity, getCounty, Province } from '../../utils/data/data';
import { dao } from '../../utils/repository';
const now = new Date()
const g30Day = 3600 * 24*1000*7
Page({
  data: {
    areaList:[],
    show: false,
    title:'',
    desc:'',
    expire:'',
    locations:'',
    tags:'',
    tagShow:false,
    dateShow:false,
    minDate: now.getTime(),
    maxDate: new Date(now.getTime() + g30Day).getTime(),
    noticeTagList: dao.getNoticeList(),
    content:'',
    options:Array<Province>()
  },
  onShow(){
    const proviceList = getChinaProvince()
    this.setData({options:proviceList})
  },
  onTagSelect(_e:any){
    this.setData({tagShow:true})
  },
  onDateSelect(_e:any){
    this.setData({dateShow:true})
  },
  onLClick(_e:any){
    this.setData({show:true})
  },
  onTagConfirm(e:any){
    const value = e.detail.value
    this.setData({tagShow:false,tags:value})
  },
  onTagCancel(_e:any){
    this.setData({tagShow:false})
  },
  onDateCancel(_e:any){
    this.setData({dateShow:false})
  },
  onDateConfirm(e:any){
    const date = new Date(e.detail)
    this.setData({dateShow:false,expire:date.toISOString().slice(0, 10)})
  },
  onLClose(_e:any){
    this.setData({show:false})
  },
  onLFinish(e:any){
    const values = e.detail.selectedOptions
    const locations = `${values[0].text}|${values[1].text}|${values[2].text}`
    this.setData({show:false,locations:locations})
  },
  onLChange(e:any){
    const { tabIndex, value } = e.detail;
    if(tabIndex === 0){
      const cityList = getCity(value)
      let options = this.data.options
      for(var i = 0; i < options.length;i++){
        if(value === options[i].value){
          options[i].children = cityList
          this.setData({options:options})
          break
        }
      }
    }else if(tabIndex === 1){
      const countrys = getCounty(value)
      const options = this.data.options
      const prefix = value.slice(0, 2)
      for(var i = 0; i < options.length;i++){
        if(!options[i].value.startsWith(prefix)){
          continue
        }
        for(var j = 0; j < options[i].children.length;j++){
          if(value === options[i].children[j].value){
            options[i].children[j].children = countrys
            this.setData({options:options})
            break
          }
        }
        break
      }
    }
  }  
})